<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="dialogData.title"
    :visible.sync="isDialog.visible"
    top="5vh"
    width="1000px"
  >
    <div class="header-box">
      <!-- 请选择账户-->
      <div>
        <el-select
          v-model="queryInfo.account"
          clearable
          placeholder="请选择账户"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.account"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <!-- 请选择变动类型-->
      <div>
        <el-select
          v-model="queryInfo.type"
          clearable
          placeholder="请选择变动类型"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.type"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <!-- 时间筛选-->
      <div>
        <el-date-picker
          v-model="searchTime"
          :unlink-panels="true"
          clearable
          end-placeholder="结束日期"
          range-separator="至"
          start-placeholder="开始日期"
          style="width: 100%"
          type="daterange"
          value-format="yyyy-MM-dd"
          @change="handleTime"
        >
        </el-date-picker>
      </div>
      <el-button type="primary" @click="handleResetPage">
        搜索
        <i class="el-icon-search el-icon--right"></i>
      </el-button>
      <el-button
        v-if="account !== 'online'"
        type="primary"
        @click="handleRecharge"
      >
        充值
      </el-button>
    </div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        header-cell-class-name="table-header"
        height="100%"
        stripe
        style="width: 100%"
      >
        <el-table-column align="center" label="账户" width="150">
          <template slot-scope="{ row }">
            {{ row.account.trans }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="变动类型" width="150">
          <template slot-scope="{ row }">
            {{ row.type.trans }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="变动金额" width="150">
          <template slot-scope="{ row }">
            {{ row.amount }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="创建时间" prop="create_time">
        </el-table-column>

        <el-table-column
          align="center"
          label="备注"
          prop="remark"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>
    <!-- 数权-->
    <Recharge
      v-if="isDialog.recharge"
      :account="filterItem.account"
      :info="info"
      :type="queryInfo.account"
      @close="closeDialog"
    ></Recharge>
  </el-dialog>
</template>

<script>
import { lists as getList } from "@/api/providerBill";
import Recharge from "./recharge";

export default {
  name: "Bill",
  components: {
    Recharge,
  },
  props: {
    info: {
      type: Object,
    },
    account: {
      type: String,
    },
    type: {
      type: String,
    },
    mode: {
      type: String,
    },
  },
  data() {
    return {
      queryInfo: {
        page: 1,
        rows: 10,
        provider_id: this.info.provider_id,
        account: this.account,
        type: this.type,
        starttime: "",
        endtime: "",
      },
      // 列表
      dataList: [],
      // 总页数
      totalCount: 0,
      // 数据
      dialogData: {
        title: "",
      },
      // 时间
      searchTime: [],
      // 筛选项
      filterItem: {
        type: [],
        account: [],
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 防抖
      queryId: +new Date(),
      // 弹窗
      isDialog: {
        visible: true,
        recharge: false,
      },
    };
  },
  created() {
    this.dialogData.title = "查看账单" + "(" + this.info.account + ")";

    this.getData();
  },
  methods: {
    // 数据
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
          this.filterItem.account = res.data.account_screen;
          this.filterItem.type = res.data.type_screen;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 充值
    handleRecharge() {
      this.isDialog.recharge = true;
    },
    // 搜索 - 筛选时间
    handleTime() {
      if (this.searchTime) {
        this.queryInfo.starttime = this.searchTime[0];
        this.queryInfo.endtime = this.searchTime[1];
      } else {
        this.queryInfo.starttime = "";
        this.queryInfo.endtime = "";
        this.searchTime = [];
      }
      this.queryInfo.page = 1;
      this.getData();
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    //关闭
    closeDialog(val) {
      if (!val) {
        this.getData();
      }
      this.isDialog.recharge = false;
    },
    // 关闭
    handleClose() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  min-height: 60vh;
}

::v-deep .el-dialog__body {
  padding: 20px 20px;
}

.header-box {
  display: flex;

  > div {
    margin-right: 10px;
  }
}

.flex-content {
  margin-top: 10px;
}
</style>
